home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 June / macformat-038.iso / Shareware in MacFormat / Creator Changer 2.5 / Code & Resource / Creator Changer.AE.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-03  |  5.8 KB  |  197 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the functions which initialize the core 
  3.  *    AppleEvents for the program.
  4.  **********************************************************************/
  5.  
  6. #include "Creator Changer.h"
  7. #include "Creator Changer.AE.h"
  8. #include "GetFileIcon.h"
  9.  
  10.  
  11. /**********************************************************************
  12.  *    Function Install_AE_Handlers(), this function sets up the 
  13.  *    AppleEvents for the program.
  14.  **********************************************************************/
  15.  
  16. void Install_AE_Handlers(void)
  17.     {
  18.     
  19.     AEEventHandlerUPP open_ae, quit_ae, start_ae, print_ae;
  20.     
  21.     open_ae=NewAEEventHandlerProc(Open_AE);
  22.     quit_ae=NewAEEventHandlerProc(Quit_AE);
  23.     start_ae=NewAEEventHandlerProc(Start_AE);
  24.     print_ae=NewAEEventHandlerProc(Print_AE);
  25.     
  26.     AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,   open_ae,  FALSE, FALSE);
  27.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, quit_ae,  FALSE, FALSE);
  28.     AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, start_ae, FALSE, FALSE);
  29.     AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,  print_ae, FALSE, FALSE);
  30.     
  31.     }
  32.  
  33.  
  34.  
  35. /**********************************************************************
  36.  *    Function Open_AE(), this function supports what happens when a file
  37.  *    is dropped on Creator Changer.
  38.  **********************************************************************/
  39.  
  40. pascal OSErr Open_AE(AppleEvent *the_event, AppleEvent *the_reply, long ref_con)
  41.     {
  42.     
  43.     AEDescList            the_files;
  44.     AEKeyword            key_word;
  45.     DescType            the_type;
  46.     Size                the_size;
  47.     auto_chng            **rsrc_handle;
  48.     File_Union            temp_text;
  49.     Str4                types[100];
  50.     short                 i, the_item=1;
  51.     OSType                file, creator;
  52.     long                num_of_files;
  53.         
  54.     AEGetParamDesc(the_event, keyDirectObject, typeAEList, &the_files);
  55.     AECountItems(&the_files, &num_of_files);
  56.     
  57.     if((**Global).AutoChange && Control_Key_Down)
  58.         {
  59.         Make_Pop_Up_Menu(AUTO_CHANGE_ID, PREF_AC, Num_ac);
  60.         Get_Types_From_Prefs(types);
  61.         for(i=1;i<=num_of_files;i++)
  62.             {
  63.             Multiple_Files=NO;
  64.             AEGetNthPtr(&the_files, i, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  65.             FSpGetFInfo(&The_File_Spec, &File_Info);
  66.             the_item=Check_File_Type(types, File_Info.fdType);
  67.             if(the_item)
  68.                 {
  69.                 UseResFile(CreatorChangerPref);
  70.                     rsrc_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[the_item]);
  71.                     Set_Long_Type((**rsrc_handle).ToFile, &File_Info.fdType, NIL_PTR, NIL_PTR);
  72.                     Set_Long_Type((**rsrc_handle).ToCreator, &File_Info.fdCreator, NIL_PTR, NIL_PTR);
  73.                     ReleaseResource((Handle)rsrc_handle);
  74.                 UseResFile(CreatorChangerApp);
  75.                 FSpSetFInfo(&The_File_Spec, &File_Info);
  76.                 Force_Finder_Update(&The_File_Spec);
  77.                 }
  78.             else
  79.                 {
  80.                 GetFileIcon(&The_File_Spec, svAllAvailableData, &The_Icons);
  81.                 Open_DLOG(&Chng_Dialog, PREF_TYPES_ID, PREF_PT, CHNG_TYPE_DLOG_ID, Num_pt, Chng_Storage, &the_files, one_file);
  82.                 }
  83.             }
  84.         AEDisposeDesc(&the_files);
  85.         }
  86.     else
  87.         {
  88.         if(num_of_files==1)
  89.             {
  90.             Multiple_Files=NO;
  91.             AEGetNthPtr(&the_files, 1, typeFSS, &key_word, &the_type, &The_File_Spec, sizeof(The_File_Spec), &the_size);
  92.             FSpGetFInfo(&The_File_Spec, &File_Info);
  93.             GetFileIcon(&The_File_Spec, svAllAvailableData, &The_Icons);
  94.             AEDisposeDesc(&the_files);
  95.             }
  96.         else Multiple_Files=YES;
  97.     
  98.         Open_DLOG(&Chng_Dialog, PREF_TYPES_ID, PREF_PT, CHNG_TYPE_DLOG_ID, Num_pt, Chng_Storage, &the_files, num_of_files);
  99.     
  100.         Multiple_Files=NO;
  101.         }
  102.     
  103.     if((**Global).DragQuit==TRUE) All_Done=TRUE;
  104.     
  105.     return(noErr);
  106.     
  107.     }
  108.  
  109.  
  110.  
  111. /**********************************************************************
  112.  *    Function Get_Types_From_Prefs(), this function gets the types from
  113.  *    preference file and stores them in an array, for later use.
  114.  **********************************************************************/
  115.  
  116. void Get_Types_From_Prefs(Str4 *the_array)
  117.     {
  118.     
  119.     short             i;
  120.     auto_chng        **the_handle;
  121.     
  122.     UseResFile(CreatorChangerPref);
  123.     the_handle=(auto_chng **)NewHandle(sizeof(auto_chng));
  124.     for(i=1;i<=Num_ac;i++)
  125.         {
  126.         the_handle=(auto_chng **)GetResource(PREF_AC, The_ac_Type[i]);
  127.         p_Str_Copy((**the_handle).FromFile, the_array[i]);
  128.         ReleaseResource((Handle)the_handle);
  129.         }
  130.     UseResFile(CreatorChangerApp);
  131.     
  132.     }
  133.  
  134.  
  135.  
  136. /**********************************************************************
  137.  *    Function Check_File_Type(), this function checks the current file's
  138.  *    file type against those in the array created before.
  139.  **********************************************************************/
  140.  
  141. int Check_File_Type(Str4 *the_arry, OSType the_ostype)
  142.     {
  143.     
  144.     short         i;
  145.     File_Union    the_union_type;
  146.     
  147.     Set_Type_Text(&the_union_type, the_ostype);
  148.     
  149.     for(i=1;i<=Num_ac;i++) if(Comp_The_String(the_arry[i], the_union_type.TEXT)) return(i);
  150.     return(BAD);
  151.     
  152.     }
  153.  
  154.  
  155.  
  156. /**********************************************************************
  157.  *    Function Quit_AE(), this function supports the 'quit' event.  All
  158.  *    that it does is set the global switch to true to quit.
  159.  **********************************************************************/
  160.  
  161. pascal OSErr Quit_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  162.     {
  163.     
  164.     All_Done=TRUE;
  165.     return(noErr);
  166.     
  167.     }
  168.  
  169.  
  170.  
  171. /**********************************************************************
  172.  *    Function Print_AE(), this function does nothing, besides report 
  173.  *    that CC does not support the print event.
  174.  **********************************************************************/
  175.  
  176. pascal OSErr Print_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  177.     {
  178.     
  179.     return(errAEEventNotHandled);
  180.     
  181.     }
  182.  
  183.  
  184.  
  185. /**********************************************************************
  186.  *    Function Start_AE(), this function controls what happens when 
  187.  * Creator Changer is launched.
  188.  **********************************************************************/
  189.  
  190. pascal OSErr Start_AE(AppleEvent *theEvent, AppleEvent *reply, long ref_con)
  191.     {
  192.     
  193.     Handle_Options_Choice(O_OPEN_ITEM);
  194.     return(noErr);
  195.         
  196.     }
  197.